-- =========================================================================================
-- 				PROTECT THE VIP - Gamemode by SteveUK
--  You are free to make modifications and redistributions of this gamemode as long as I'm credited for it
--=========================================================================================

-- checks for winning conditions
function GM:GamerulesThink()

	local bVIPIsDead = false
	local iAssassinsAliveCount = 0
	local iAssassinsOverallCount = 0
	
	for k, v in pairs( player.GetAll() ) do
		if( v:Team() == TEAM_VIP and v:Alive() == false ) then -- Assassin killed a VIP
			SetGlobalBool( "VIPIsDead", true )
			SetGlobalBool( "RoundOver", true )
		elseif( v:Team() == TEAM_VIP and v:GetNetworkedBool( "IsInRescue", false ) == true ) then -- VIP got to rescue point
			SetGlobalBool( "VIPEscaped", true )
			SetGlobalBool( "RoundOver", true )
		elseif( v:Team() == TEAM_ASSASSIN ) then -- they're on assassin team
			iAssassinOverallCount = iAssassinOverallCount + 1
			if( v:Alive() == true ) then -- increase the alive assassin count.
				iAssassinsAliveCount = iAssassinsAliveCount + 1
			end
		end
	end
end

-- set up scores for a player
function GM:ScorePlayer( pl, attacker )

	if( attacker:Team() == TEAM_PROTECTORS and pl:Team() == TEAM_VIP ) then
		-- penalise the protector for killing the VIP
		attacker:AddFrags( -2 )
		SetGlobalBool( "VIPWasTKed", true )
		SetGlobalString( "VIPKillerName", pl:Nick() )
		SetGlobalEntity( "VIPKillerEnt", pl )
	elseif( pl:Team() == TEAM_VIP ) then
		-- someone killed the VIP, this gets them 3 frags
		attacker:AddFrags( 3 )
		SetGlobalBool( "VIPWasTKed", false )
		SetGlobalString( "VIPKillerName", pl:Nick() )
		SetGlobalEntity( "VIPKillerEnt", pl )
	elseif( GAMEMODE:IsAlly( pl, attacker ) == true ) then
		-- player killed an ally
		attacker:AddFrags( -1 ) -- punish
	else
		-- player killed no one special
		attacker:AddFrags( 1 )
	end

end
